package com.neouton.crm.screen.Hander;

import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
public class MapResultHandler<K,V> implements ResultHandler<Map<K,V>> {
    private final Map<K,V> mappedResults = new HashMap<>();

    @Override
    public void handleResult(ResultContext context) {
        Map map = (Map) context.getResultObject();
        if( (V)map.get("value") == null){
            BigDecimal bigDecimal = new BigDecimal(0.00);
            bigDecimal.setScale(2, RoundingMode.HALF_DOWN );
            mappedResults.put((K)map.get("key"), (V)bigDecimal);
        }else {
            if(map.get("value") instanceof BigDecimal){
                mappedResults.put((K)map.get("key"), (V)map.get("value"));
            }else {
                BigDecimal bigDecimal = new BigDecimal((Double) map.get("value"));
                bigDecimal.setScale(2,RoundingMode.HALF_DOWN );
                mappedResults.put((K)map.get("key"), (V)bigDecimal);
            }
        }
    }

    public Map<K,V> getMappedResults() {
        return mappedResults;
    }
}
